package cn.edt.ctbu.thymeleaf.webapi;

import cn.edt.ctbu.thymeleaf.domain.Student;
import cn.edt.ctbu.thymeleaf.service.StudentService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/webapi/login")
public class LoginRestController {
    @Autowired
    StudentService studentService;

    @GetMapping("/user")
    public int user(HttpServletRequest httpreqeust,String username,String password){
        //判断用户名和密码是否存在
        List<Student> students=studentService.findByNameAndPassword(username,password);
        if (students.size()>0){
            //写入session
            HttpSession session=httpreqeust.getSession();
            session.setAttribute("userid",students.get(0).getId());
            session.setAttribute("user",students.get(0));
            return 1;//成功
        }else{
            //如果不存在，返回出错
            return -1;//不存在用户
        }
    }
}
